package com.turnengine.client.global.translation.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.javabi.common.locale.LanguageCountry;
import java.io.IOException;

/**
 * The Set Translation Data Serializer.
 */
public class SetTranslationDataSerializer extends AbstractCommandDataSerializer<Integer> implements ISetTranslationDataSerializer {

	/**
	 * Creates a new Set Translation Data Serializer.
	 */
	public SetTranslationDataSerializer() {
		super(SetTranslation.COMMAND_ID);
	}

	@Override
	public Integer readReturnValue(IDataReader reader) throws IOException {
		Integer value = reader.readIntegerObject(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, Integer value) throws IOException {
		writer.writeIntegerObject(value, true);
	}

	@Override
	public IExecutableCommand<Integer> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int id = reader.readInt();
		LanguageCountry language = reader.readEnum(LanguageCountry.class, true);
		String translation = reader.readString(true);

		// Create object
		SetTranslation object = new SetTranslation();
		object.setLoginId(loginId);
		object.setId(id);
		object.setLanguage(language);
		object.setTranslation(translation);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<Integer> argument) throws IOException {
		SetTranslation object = (SetTranslation) argument;

		// Get fields
		long loginId = object.getLoginId();
		int id = object.getId();
		LanguageCountry language = object.getLanguage();
		String translation = object.getTranslation();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(id);
		writer.writeEnum(language, true);
		writer.writeString(translation, true);
	}

	@Override
	public IExecutableCommandResponse<Integer> newResponse() {
		return new SetTranslationResponse();
	}
}
